package com.ccn.kotlin_mvvm.ui

import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.viewpager2.widget.ViewPager2
import com.ccn.jetpack_mvvm.base.viewmodel.BaseViewModel
import com.ccn.jetpack_mvvm.ext.view.gone
import com.ccn.jetpack_mvvm.ext.view.visible
import com.ccn.kotlin_mvvm.MainActivity
import com.ccn.kotlin_mvvm.R
import com.ccn.kotlin_mvvm.app.base.BaseActivity
import com.ccn.kotlin_mvvm.app.util.CacheUtil
import com.ccn.kotlin_mvvm.app.wiget.banner.WelcomBannerAdapter
import com.ccn.kotlin_mvvm.app.wiget.banner.WelcomBannerViewHolder
import com.ccn.kotlin_mvvm.databinding.ActivityWelcomeBinding
import com.zhpan.bannerview.BannerViewPager

class WelcomeActivity : BaseActivity<BaseViewModel, ActivityWelcomeBinding>() {

    private var resList = arrayOf("来呀", "快活呀", "反正有", "大把时光")

    private lateinit var mBaseView: ConstraintLayout
    private lateinit var mViewPager: BannerViewPager<String, WelcomBannerViewHolder>
    private lateinit var mWelcomeImage: ImageView
    private lateinit var mWelcomeJoin: TextView

    override fun layoutId(): Int = R.layout.activity_welcome

    override fun initView(savedInstanceState: Bundle?) {
        //防止出现按Home键返回桌面，再次点击重新进入该界面bug
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
        mDatabind.click = ProxyClick()
        mBaseView = findViewById(R.id.welcome_baseview)
        mViewPager = findViewById(R.id.banner_view)
        mWelcomeImage = findViewById(R.id.welcome_image)
        mWelcomeJoin = findViewById(R.id.welcomeJoin)
        if (CacheUtil.isFirst()) {
            //第一次打开
            mWelcomeImage.gone()
            mViewPager.apply {
                adapter = WelcomBannerAdapter()
                setLifecycleRegistry(lifecycle)
                registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        super.onPageSelected(position)
                        if (position == resList.size - 1) {
                            mWelcomeJoin.visible()
                        } else {
                            mWelcomeJoin.gone()
                        }
                    }
                })
                create(resList.toList())
            }
        } else {
            //不是第一次打开
            mWelcomeImage.visible()
            mViewPager.postDelayed({
                startActivity(Intent(this, MainActivity::class.java))
                finish()
                overridePendingTransition(
                    android.R.anim.fade_in,
                    android.R.anim.fade_out
                )
            }, 1000);
        }
    }

    inner class ProxyClick {
        fun toMain() {
            CacheUtil.setFirst(false)
            startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
            finish()
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }
    }
}